﻿## kill Main Install if Terminate = $true
if ($env:terminate -eq $true) {exit 0}

$env:mosssetuppath = "$env:mosssetuppath\MOSS"

Write-Host -ForegroundColor Yellow "MOSS: Installing MOSS."

#Write-Host "Checking to see if MOSS has been installed. Looking at Regkey HKEY_Local_Machine:\SOFTWARE\Microsoft\Office Server\14.0\. If Exist, then MOSS has been installed"

##
##MSIT ISEG - Start
## checks if MOSS has already been installed, if so exit and show Build Version.
if (Test-Path "hklm:\SOFTWARE\Microsoft\Office Server\14.0"){

$key = Get-Item "hklm:\SOFTWARE\Microsoft\Office Server\14.0\"
$values = Get-ItemProperty $key.PSPath
Write-Host -ForegroundColor Yellow "MOSS is already installed, Version" $values.BuildVersion
exit 0
} else {
#Install MOSS
}
##MSIT ISEG - End

## strange bug - Windows Update Service is not running - Workaround
#get-Service wuauserv 
set-Service -name wuauserv -startupType Automatic | Out-Null
start-Service wuauserv | Out-Null
## end workaround

start-Service  msiserver  | Out-Null
 

Function InstallServer
{
Param( [string]$SetupPath, [string]$servername )
	Write-Host "Installing $servername from $SetupPath...";
	
	if( ! (Test-Path $SetupPath ) )
	{
		Write-Error " $SetupPath does not exist or not accessible!"
		return "false";
	}
	
	$appconfig = "$env:temp\" + "$servername" + "_config-app.xml";
	$wfeconfig = "$env:temp\" + "$servername" + "_config-wfe.xml";
	$wfeconfigtmp = "$env:temp\" + "$servername" + "_config-wfe1.xml";
	
	copy-item $SetupPath\Files\SetupFarmSilent\config.xml $appconfig;
	if( $? -eq $false )
	{
		Write-Host -ForegroundColor Red "Copy file failed."
		return "false";
	}

	copy-item $SetupPath\Files\SetupFarmSilent\config.xml $wfeconfigtmp
	if( $? -eq $false )
	{
		Write-Host -ForegroundColor Red "Copy file failed."
		return "false";
	}


	## Prepare for the WFE silent install xml file
	#get-content -path $wfeconfigtmp | foreach {$_ -replace "APPLICATION", "WFE"} | set-content $wfeconfig
	

	##MSIT-ISEG - Change ##

	[xml]$xmldoc = get-Content $appconfig


	$node = $xmldoc.CreateElement("INSTALLLOCATION")
	$node.setAttribute("Value", "C:\Program Files\Microsoft Office Servers") | Out-Null

	
	$xmldoc.Configuration.AppendChild($node)| Out-Null


	$node = $xmldoc.CreateElement("DATADIR")
	

	$node.setAttribute("Value", "C:\SearchIndexes") | Out-Null
	$xmldoc.Configuration.AppendChild($node)| Out-Null


	$node = $xmldoc.CreateElement("PIDKEY")
	$node.setAttribute("Value", "$env:SharePointPID") | Out-Null
	$xmldoc.Configuration.AppendChild($node)| Out-Null
	
	$xmldoc.Save($appconfig)

	##END - MSIT-ISEG - Change ##

	echo "Installing APP version..."
	& $SetupPath\setup.exe /config $appconfig | Out-Host
	


	##END MSIT-ISEG - Change ##

	echo "last exit code is $lastexitcode"
	# check the exit code
	if( $lastexitcode -eq 0 )
	{
		Write-Host -ForegroundColor Yellow "MOSS: Installing MOSS. COMPLETED"

	}
	##MSIT-ISEG - Change ##
	elseif( $lastexitcode -eq 30030 )
	{
		Write-Error "A restart is needed."
		$env:terminate = $true
		$env:restart = $true
		return "false";
	}
	elseif( $lastexitcode -eq 30066 )
	{
		Write-Error "A restart is needed."
		$env:terminate = $true
		$env:restart = $true
		return "false";
	}
	## 30066
	else
	{
		write-error "installation failed. return code $lastexitcode";
		$env:terminate = $true
		$env:restart = $true
		return "false";
	}
	
	return "true";
}


# Install MOSS
if( $env:mosssetuppath -ne $null)
{
	& $env:mosssetuppath\PrerequisiteInstaller.exe /unattended | Out-Host
	if( $lastexitcode -eq 0 )
	{
		Write-Host "Prereq successfully installed!"
	}
	if( $lastexitcode -eq 1 )
	{
		Write-Error "Another instance of this application is running!"
		$env:terminate=$true
		exit 1;
	}
	elseif ( $lastexitcode -eq 1001 )
	{
		Write-Error "A pending restart blocks installation!"
		$env:terminate=$true
		$env:restart = $true
		exit 1;
	}
	elseif( $lastexitcode -eq 3010 )
	{
		Write-Error "A restart is needed."
		$env:terminate=$true
		$env:restart = $true
		exit 1;
	}
	$ret = (InstallServer $env:mosssetuppath "MOSS")
	echo $ret
	if( $ret -match "false" ) 
	{
		exit 1;
	}
}
else
{
	Write-Error "moss setup path not specified..."
	exit 1;
}

exit 0;


###########################################################################
#  
#   End
#
###########################################################################

 